package q844_backspaceCompare;

public class Solution_1 {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        sb.append("123");
        sb.delete(sb.length() - 1, sb.length());
        System.out.println(sb);
    }
    /*
    比较含有退格的字符串
    最简单的做法：用stringBuilder 遇到退格符号就删除掉最后一个字符即可
    最后比较两者是否相等
     */
    public boolean backspaceCompare(String s, String t) {
        StringBuilder sb1 = new StringBuilder(), sb2 = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '#' && sb1.length() > 0) {
                sb1.delete(sb1.length() - 1, sb1.length());
            }
            if (s.charAt(i) != '#') {
                sb1.append(s.charAt(i));
            }
        }
        for (int i = 0; i < t.length(); i++) {
            if (t.charAt(i) == '#' && sb2.length() > 0) {
                sb2.delete(sb2.length() - 1, sb2.length());
            }
            if (t.charAt(i) != '#') {
                sb2.append(t.charAt(i));
            }
        }

        return sb1.toString().equals(sb2.toString());
    }
}
